package cc.rengu.igas.channel.wechat.core.service.trans;


import cc.rengu.igas.channel.wechat.common.constant.WechatParamConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatTreeNodeConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.wechat.core.service.base.MchntService;
import cc.rengu.igas.channel.wechat.facade.request.WechatUpdateMerchantRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatUpdateMerchantResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;


/**
 * 更新商户信息
 */
public class WechatUpdateMerchantService extends MchntService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        WechatUpdateMerchantRequest updateMerchantRequest = new WechatUpdateMerchantRequest();
        ConvertUtil.convertOutput(updateMerchantRequest);
        return updateMerchantRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;

        WechatUpdateMerchantResponse updateMerchantResponse = new WechatUpdateMerchantResponse();
        WechatUpdateMerchantRequest updateMerchantRequest = (WechatUpdateMerchantRequest) request;
        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);

        JSONObject bankCardInfo = new JSONObject();
        bankCardInfo.put("card_no", updateMerchantRequest.getBankcardInfo().getCardNo());
        bankCardInfo.put("card_name", updateMerchantRequest.getBankcardInfo().getCardName());
        bankCardInfo.put("bank_branch_name", updateMerchantRequest.getBankcardInfo().getBankBranchName());

        JSONObject addressInfo = new JSONObject();
        addressInfo.put("province_code", updateMerchantRequest.getAddressInfo().getProvinceCode());
        addressInfo.put("city_code", updateMerchantRequest.getAddressInfo().getCityCode());
        addressInfo.put("district_code", updateMerchantRequest.getAddressInfo().getDistrictCode());
        addressInfo.put("address", updateMerchantRequest.getAddressInfo().getAddress());

        ConvertUtil.convertInput(updateMerchantRequest);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, updateMerchantRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_CHANNEL_NO, dstChannelCfg.getChannelNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_MCHNT_NO, dstChannelCfg.getProviderMchntNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_APP_ID, dstChannelCfg.getAppId());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.SERVICE_CODES, "[JSAPI,MICROPAY]");
        if (!StringUtil.isEmptyOrNull(dstChannelCfg.getOtherConfParam())) {
            JSONObject jsonObject = JSONObject.parseObject(dstChannelCfg.getOtherConfParam());
            xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_CHANNEL_NAME, jsonObject.getString(WechatTreeNodeConstant.WX_CHANNEL_NAME));
        }
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_ADDRESS_INFO, addressInfo.toString());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_BANKCARD_INFO, bankCardInfo.toString());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (xmlTreeUtil.isExist(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE)) {
            xmlTreeUtil.deleteXmlTreeNode(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE);
        }
        if (0 != iReturnCode && 98 != iReturnCode) {
            rglog.error("调用微信服<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        } else if (98 == iReturnCode) {
            rglog.error("调用微信服<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(updateMerchantResponse);
        String resultCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RESULT_CODE);
        String returnCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_CODE);
        String returnMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_MSG);
        String errCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_CODE);
        String errMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_MSG);
        if (!WechatParamConstant.STRING_SUCCESS.equals(resultCode)) {
            updateMerchantResponse.setRespCode((null == errCode) ? returnCode : errCode);
            updateMerchantResponse.setRespDesc((null == errMsg) ? returnMsg : errMsg);
            rglog.error("调用第三方服务失败,respCode=[{}],respDesc=[{}]", updateMerchantResponse.getRespCode(), updateMerchantResponse.getRespDesc());
        } else {
            updateMerchantResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            updateMerchantResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(updateMerchantResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        WechatUpdateMerchantResponse updateMerchantResponse = (WechatUpdateMerchantResponse) response.getResult();
        ConvertUtil.convertInput(updateMerchantResponse);
    }

}
